/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.model;
/**
 * 说明：
 * 商品管理model 基本数据
 * @Author：李智深
 * @Date：2012-04-01
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.ruipengkj.bean.TCommodity;

public class DataCommodityPackageModel extends AbstractTableModel{

	private static final long serialVersionUID = 1L;
	private Vector<String> columns = new Vector<String>();
	private List<TCommodity> tCommodityList=new ArrayList<TCommodity>();
	private List<TCommodity> delCommodityList;
	public DataCommodityPackageModel(List<TCommodity> delCommodityList){
		columns.add("商品编号");
		columns.add("商品名称");
		columns.add("商品条码");
		columns.add("价格");
		columns.add("现价");
		columns.add("批发价");
		this.delCommodityList = delCommodityList;
	}
	//获得行数
	@Override
	public int getRowCount(){
		return tCommodityList.size();
	}
	//获得列数
	@Override
	public int getColumnCount(){
		return columns.size();
	}
	//获得单元格的值
	public Object getValueAt(int row,int column){
		TCommodity p = (TCommodity) tCommodityList.get(row);
		return getTCommodityValueByCol(p, column);
	}
	public Object getTCommodityValueByCol(TCommodity tCommodity, int columnIndex) {
		switch (columnIndex) {
			case 0:
				return tCommodity.getCommodityID();
			case 1:
				return tCommodity.getName();
			case 2:
				return tCommodity.getBarcode();
			case 3:
				return tCommodity.getPrice();
			case 4:
				return tCommodity.getDiscountPrice();
			case 5:
				return tCommodity.getBatchPrice();
		}
		return null;
	}
	//获取列名
	@Override
	public String getColumnName(int column){
		return (String)columns.get(column);
	}

	//设置单元格是否可编辑
	@Override
	public boolean isCellEditable(int rowIndex,int columnIndex){
		if (columnIndex < 3)
			return true;
		return false;
	}
	
	//设置单元格的值
	@Override
	public void setValueAt(Object aValue,int rowIndex,int columnIndex){
		if(aValue instanceof TCommodity){
			if (tCommodityList.get(rowIndex).getSuitDetailsID() != null)
				delCommodityList.add(tCommodityList.get(rowIndex));
			tCommodityList.remove(rowIndex);
			tCommodityList.add(rowIndex, (TCommodity)aValue);
			this.fireTableRowsUpdated(rowIndex, rowIndex);
		}
	}
	public void addRow(TCommodity p) {
		tCommodityList.add(p);
		fireTableRowsInserted(tCommodityList.size()-1, tCommodityList.size()-1);
	}
	public boolean deleteRow(int index) {
		if (index >= 0 && index < tCommodityList.size()) {
			tCommodityList.remove(index);
			fireTableRowsDeleted(index, index);
			return true;
		} else
			return false;
	}
	
	public TCommodity getTCommodity(int row) {
		return tCommodityList.get(row);
	}
	
	public List<TCommodity> getTCommodityList() {
		return tCommodityList;
	}
	
	public void addList(List<TCommodity> tCommodityList){
		if(tCommodityList!=null){
			this.tCommodityList=tCommodityList;
		}
	}
}